package auctionhaus

import com.icegreen.greenmail.util.*

/**
 * Created with IntelliJ IDEA.
 * User: Chris Seeling
 * Date: 4/14/12
 * Time: 12:18 PM
 * To change this template use File | Settings | File Templates.
 */
class CompletedListingsIntegrationTests extends GroovyTestCase {
    def greenMail
    def mailService
    def bidService
    def listingService
    def jmsService
    def listing
    def seller
    def bidder

    public void setUp() {
        seller = new Customer(
                email: "johndoe@acme.com",
                password: "asdfwe",
                //role: "user"
        ).save(failOnError: true)
        bidder = new Customer(
                email: "newuser@acme.com",
                password: "asdfwe",
                //role: "user"
        ).save(failOnError: true)
        listing = new Listing(name: "Listing 2",
                endTime: new Date()+2,
                description: "blah blah blah",
                seller: seller,
                startBid: 5,
                minBid: 5).save(failOnError: true)

        if(!listing.bids) {
            listing.bids = new HashSet<Bid>()
        }
        def newBid = new Bid(
                amount: 6,
                bidder:  bidder
        )
        listing.bids.add(newBid)
        newBid.listing = listing
        listing.save(flush: true)
    }

    void testAddBidExpiredListing() {
        listing.isComplete = true
        listing.save(flush: true)

        def bidInstance = bidService.addBid(listing, seller, 10)
        assertNull bidInstance.id
        assert bidInstance.listing.hasErrors()
    }

    void testJMSMessage() {
        def testDate = new Date()
        testDate.seconds = testDate.seconds + 0.1
        listing.endTime = testDate
        listing.save(flush: true)
        (0..<50).each {
            //do some random stuff to wait for a second
            def it2 = it*it+it-it
        }
        listingService.postCompletedListings()
        def messages = jmsService.browse(queue: 'completedListingsQueue', null, null)

        assert messages[0].toString().contains("\"isComplete\": true")
        assert listing.winner.email == "newuser@acme.com"
    }

    /*
    void testSendMail() {
        Map mail = [message:'hello world', from:'admin@auctionhaus.com', to:'user@gmail.com', subject:'subject']

        mailService.sendMail {
            to mail.to
            from mail.from
            subject mail.subject
            body mail.message
        }

        assertEquals(1, greenMail.getReceivedMessages().length)

        def message = greenMail.getReceivedMessages()[0]

        assertEquals(mail.message, GreenMailUtil.getBody(message))
        assertEquals(mail.from, GreenMailUtil.getAddressList(message.from))
        assertEquals(mail.subject, message.subject)
    }

    void testEmail() {
        def testDate = new Date()
        testDate.seconds = testDate.seconds + 0.1
        listing.endTime = testDate
        listing.save(flush: true)
        (0..<50).each {
            //do some random stuff to wait for a second
            def it2 = it*it+it-it
        }
        listingService.postCompletedListings()
        assert listing.winner
        listingService.notifyWinner(listing)
        assertEquals(1, greenMail.getReceivedMessages().length)
    }
    */

    public void tearDown() {
        seller = null
        listing = null
        greenMail.deleteAllMessages()
    }
}
